package logic

import (
	"context"
	"errors"
	"video-up-manager/models/mysql"
	"video-up-manager/videorpc/internal/svc"
	"video-up-manager/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoUpdateLogic {
	return &VideoUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VideoUpdateLogic) VideoUpdate(in *videorpc.VideoUpdateRequest) (*videorpc.VideoUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var video mysql.Video

	if err := video.FindVideoByTitle(mysql.DB, in.VideoId); err != nil {
		return nil, errors.New("视频不存在")
	}
	video.VideoStatus = in.VideoStatus

	if err := video.VideoUpdate(mysql.DB, in.VideoId); err != nil {
		return nil, err
	}
	if in.VideoStatus == "2" {
		if err := video.VideoDel(mysql.DB, in.VideoId); err != nil {
			return nil, err
		}
	}

	return &videorpc.VideoUpdateResponse{
		VideoId: int64(video.ID),
	}, nil
}
